import { GetBusinessinfoAPI, PostSaveBusinessinfoAPI } from "@/api/FormElements";
import store from '@/store/index'

export const iframeDispose = async (data, formData) => {
  console.log('iframe传过来：', data);
  if (!data.type) return

  let apiGlobalParameter, res

  switch (data.type) {
    case 'addForm':
      res = await addForm(formData, data)
      if (res?.result_code === 1) {
        iframeReturn({ type: 'addForm', id: res.out_put_value.business_info_id })
      } else {
        return
      }
      console.log(res);

      break;
    case 'setApiGlobalParameter':
      apiGlobalParameter = JSON.parse(data.apiGlobalParameter)
      store.commit('globalParameter/setApiGlobalParameter', apiGlobalParameter)
      console.log(20, apiGlobalParameter);
      break
    case 'lookOver':
      res = await getBusinessinfo(data.id)
      if (res?.result_code === 1) {
        console.log('获取表单数据', res);
        return res.out_put_value
      }
      return
    default:
      break;
  }
}




export const iframeReturn = ({ type, ...data }) => {
  console.log('传到父级', { type, ...data, });
  switch (type) {
    case 'addForm':

      break;

    default:
      break;
  }

  console.log(777777777777777777777);
  window.parent.postMessage({
    type,
    data: JSON.stringify(data)
  }, '*')
}

/**
 * 新增表单
 * @param {Object} formData 表单数据
 * @param {Object} data iframe 传入的数据
 * @returns 
 */
export const addForm = async (formData, data) => {
  console.log(27, store.getters);
  const apiGlobalParameter = store.getters.apiGlobalParameter
  const res = await PostSaveBusinessinfoAPI({
    dept_id: 2,
    // dept_id: apiGlobalParameter.dept_id,
    dept_nm: apiGlobalParameter.dept_nm,
    model_main: {
      form_id: 1,
      form_type_id: 1,
      style_type: 0,
      title: data.title,
      form_content: JSON.stringify(formData)
    },
    data_main: { bill_content: [] }
  })
  console.log(53, res);
  switch (res.result_code) {
    case 1:
      return res
    default:
      break;
  }
}

export const getBusinessinfo = async (id)=> {
  const res = await GetBusinessinfoAPI(id)
  switch (res.result_code) {
    case 1:
      return res
    default:
      break;
  }
}


